package org.jaaksi.demo.gb;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

/**
 * 1.如果是类似qq断网显示提醒，就在onStart中注册，在onPause或onStop中销毁，
 * 2.如果是视频播放进入后台时网络状态切换时，不提示，onResume时提示，就在onCreate中注册，在onDestroy中销毁
 * 3.网络变化广播在注册和受到广播的时候都会调用
 */
public class MyActivity extends Activity implements MyGbReceiver.ReceiveListener, View.OnClickListener {

    private MyGbReceiver receiver;
    private View alertBar;

    @Override
    protected void onStart() {
        super.onStart();
        // TODO 一般在 onStart()中动态注册广播。根据需求选择 注册与注销广播的位置
        registerGb();
    }


    /**
     * 注册广播
     */
    public void registerGb() {
        receiver = new MyGbReceiver(this);
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        // 持有相应的action才能接收相应的广播
//        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, filter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

//        registerGb();
        findViewById(R.id.btn_next).setOnClickListener(this);
        findViewById(R.id.btn_send).setOnClickListener(this);
        alertBar = findViewById(R.id.bar_alert_no_net);
        alertBar.setOnClickListener(this);

    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(receiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(receiver);
    }

    /**
     * 收到广播后回调
     */
    @Override
    public void callBack() {
        Toast.makeText(this, "收到广播，回调给Activity处理", Toast.LENGTH_SHORT).show();
        // TODO 处理
    }

    @Override
    public void isNetIn(boolean isNetIn) {
        // 是否有网络
        if (isNetIn) {
            if (alertBar.getVisibility() == View.VISIBLE) {
                alertBar.setVisibility(View.GONE);
            }
        } else {
            if (alertBar.getVisibility() == View.GONE) {
                alertBar.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                //        Intent intent = new Intent(this,MyGbReceiver.class);
                Intent intent = new Intent("my_action");
//                Intent intent = new Intent();
//                intent.setAction("my_action");
                intent.putExtra("msg", "你好");
                // 发送无序广播
                sendBroadcast(intent);
                break;
            case R.id.btn_next:
                Intent intent1 = new Intent(this, OtherActivity.class);
                startActivity(intent1);
                break;
            case R.id.bar_alert_no_net:
                // TODO 设置网络
                Intent intent2 = null;
                // TODO
                if (Build.VERSION.SDK_INT > 10) { // 3.0以上
                    intent2 = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                } else {
                    intent2 = new Intent();
                    intent2.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
                }

                startActivity(intent2);
                break;
        }
    }
}
